package com.lifo.good.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import com.lifo.good.bean.model.HfCategory;
import com.lifo.good.bean.model.HfGoodDescription;
import com.lifo.good.bean.model.HfGoods;
import com.lifo.good.service.SearchGoodCategoryService;
import com.lifo.good.service.SearchGoodsService;
import com.lifo.good.service.SearchHfGoodDescriptionService;
import com.lifo.utils.common.messagehandler.JsonStandardMsgBean;
import com.lifo.utils.wrapper.WrapperJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2018/5/8.
 */
@Slf4j
@RestController
@RequestMapping(value = "/good")
public class HfGoodController {

    @Autowired
    private SearchGoodsService searchGoodsService;

    @Autowired
    private SearchHfGoodDescriptionService searchHfGoodDescriptionService;

    @Autowired
    private SearchGoodCategoryService searchGoodCategoryService;

    @RequestMapping(value = "/goodInfo")
    public String goodSku(String hfUid, Model model) {
        return "good/goodinfo";
    }

    @RequestMapping(value = "/showDetail", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String showDetail(@RequestBody HfGoods hfGoods){
        HfGoodDescription hfGoodDescription = searchHfGoodDescriptionService.searchHfGoodDescriptionByHfUid(hfGoods.getHfUid());

      /*  log.debug("控制层获取要单个商品sku==>{}", hfGood);
        model.addAttribute("hfGood", hfGood);
        String[] size = hfGood.getSize().split(";");
        String[] color = hfGood.getColor().split(";");
        String[] finalSmallPicture = hfGood.getSmallPicture().split(";");
        model.addAttribute("sizes", size);
        model.addAttribute("colors", color);
        model.addAttribute("finalSmallPictures", finalSmallPicture);*/
        return JsonStandardMsgBean.getMsgJsonWithData(hfGoodDescription);
    }

    @RequestMapping(value = "/showSku", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public String showSku(@RequestBody HfGoods hfGoods){
        HfGoods hfGood = searchGoodsService.searchHfGoodsByHfUid(hfGoods.getHfUid());
      /*  log.debug("控制层获取要单个商品sku==>{}", hfGood);
        model.addAttribute("hfGood", hfGood);
        String[] size = hfGood.getSize().split(";");
        String[] color = hfGood.getColor().split(";");
        String[] finalSmallPicture = hfGood.getSmallPicture().split(";");
        model.addAttribute("sizes", size);
        model.addAttribute("colors", color);
        model.addAttribute("finalSmallPictures", finalSmallPicture);*/
        return JsonStandardMsgBean.getMsgJsonWithData(hfGood);
    }

    @RequestMapping(value = "/moreGoodInfo")
    @ResponseBody
    public String goodDetailInfo(String hfUid) throws JsonProcessingException {
        HfGoodDescription hfGoodDescription = searchHfGoodDescriptionService.searchHfGoodDescriptionByHfUid(Integer.parseInt(hfUid));
        log.debug("控制层获取要单个商品详情==>{}", hfGoodDescription);
        return new WrapperJson().toJson(hfGoodDescription);
    }

    @RequestMapping(value = "/showcaseInfo")
    @ResponseBody
    public String showcaseInfo(String shopId) throws JsonProcessingException {
        List<HfGoods> hfGoodses = searchGoodsService.searchShowcaseGood(Integer.parseInt(shopId), 7);
        log.debug("展示橱窗商品==>{}", hfGoodses);
        return new WrapperJson().toJson(hfGoodses);
    }

    /**
     * 根据类别和分页查询商品
     *
     * @param categoryId
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/showGoodByCategory")
    @ResponseBody
    public String showGoodByCategory(long categoryId)   {
        PageInfo<HfGoods> hfGoodses = searchGoodsService.searchHfGoodsByCategory(categoryId,1,15);
        log.debug("展示橱窗商品==>{}", hfGoodses);
        return JSON.toJSONString(hfGoodses);
    }


    @RequestMapping(value = "/showGoodList")
    public String showGoodList(long categoryId,Model model) throws JsonProcessingException {
        PageInfo<HfGoods> hfGoodses = searchGoodsService.searchHfGoodsByCategory(categoryId,1,15);
       // model.addAttribute("hfGoodses", hfGoodses.getRows());
        List<HfCategory> categories = searchGoodCategoryService.searchHfCategory();
        model.addAttribute("categories", categories);
        return null;
    }

}
